package com.zzw.basic.jdk8;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.toList;

/**
 * @author zzw
 * @see
 * @since 2018/2/8
 */
public class StreamTest {

    @Test
    public void test1(){
        List<Integer> numbers = Arrays.asList(1,2,3,4,5,6);
        List<Integer> results = numbers.stream()
                .filter(n -> {
                    System.out.println("Filtering" + n);
                    return n % 2 == 0;
                }).map(n -> {
                    System.out.println("mapping" + n);
                    return n * n;
                }).limit(2)
                .collect(toList());
        System.out.println(results);
    }

    @Test
    public void test2(){
        List<Integer> numbers = Arrays.asList(1,2,3,4,5,6);
        Integer result = numbers.stream().reduce(0, (a, b) -> a + b);
        System.out.println(result);
    }

    @Test
    public void test3(){
        IntStream intStream = IntStream.rangeClosed(10, 30)
                .filter(n -> n % 2 == 1);
        System.out.println(intStream);
    }

    /*
      集合中获取一个Stream
      使用数值、数组或者文件创建一个Stream
      用一个函数生成一个无穷尽的Stream
     */
    @Test
    public void createSteam(){

    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        List<String> collect = list.stream().collect(toList());
    }
}
